package cn.com.ghy.android.widget.chat;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.NewCustomerInfoBean;
import cn.com.ghy.android.bean.YeTaiTypeBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.CustOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

public class NewCustomer extends Activity {
	
	private EditText txtCustomerName = null;
	private EditText txtAddr = null;
	private EditText txtCity = null;
	private EditText txtCounty = null;
	private EditText txtJdb = null;
	private EditText txtContacts = null;
	private EditText txtPhone = null;
	private Spinner spiYeTai = null;
	private EditText txtArea = null;
	private EditText txtRemark = null;
	private Button btnSubmit = null;
	private NewCustomerInfoBean newCustomerInfoBean = new NewCustomerInfoBean();
	private YeTaiTypeBean yeTaiTypeBean = null;
	private ProgressDialog progressDialog = null;
	
	private Intent intent = null;
	private Bundle bundle = null;
	private List<YeTaiTypeBean> listYeTaiType = null;
	private LoginInfoBean loginInfo = null;
	
	private DisposeException disposeException = null;
	
	private View appToolBar = null;
	private ImageButton btnList = null;
	private ImageButton btnBack = null;
	
	private HttpThread thread = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_customer);
		getSession();
		setToolBar();
		findView();
		init();
		addListener();
	}
	
	@SuppressWarnings("unchecked")
	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
		intent = this.getIntent();
		listYeTaiType = (List<YeTaiTypeBean>) intent.getSerializableExtra("listYeTaiType");
	}
	private void findView() {
		txtCustomerName = (EditText) findViewById(R.id.txtCustomerName);
		txtAddr = (EditText) findViewById(R.id.txtAddr);
		txtCity =(EditText) findViewById(R.id.txtCity);
		txtCounty = (EditText) findViewById(R.id.txtCounty);
		txtJdb = (EditText) findViewById(R.id.txtJdb);
		txtContacts = (EditText) findViewById(R.id.txtContacts);
		txtPhone = (EditText) findViewById(R.id.txtPhone);
		spiYeTai = (Spinner) findViewById(R.id.spiYeTai);
		txtArea = (EditText) findViewById(R.id.txtArea);
		txtRemark = (EditText) findViewById(R.id.txtRemark);
		btnSubmit = (Button) findViewById(R.id.btnSubmit);
	}
	
    private void setToolBar() {
    	appToolBar = findViewById(R.id.appToolBar);
    	btnList = (ImageButton) appToolBar.findViewById(R.id.btnList);
    	btnList.setVisibility(View.GONE);
    	btnBack = (ImageButton) appToolBar.findViewById(R.id.btnBack);
    }
	
	private void init() {
		ArrayAdapter<YeTaiTypeBean> yeTaiTypeAdapter = new ArrayAdapter<YeTaiTypeBean>(this, 
				android.R.layout.simple_spinner_item, listYeTaiType);
		yeTaiTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiYeTai.setAdapter(yeTaiTypeAdapter);
	}
	
	private void addListener() {
		txtArea.addTextChangedListener(txtAreaListener);
		btnSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Button button = (Button) v;
				switch(button.getId()) {
					case R.id.btnSubmit :
						if(!getData()) {
							return;
						}
						thread = new HttpThread(handler);
						thread.doStart();
						break;
				}
			}
		});
		
		spiYeTai.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				yeTaiTypeBean = (YeTaiTypeBean) parent.getItemAtPosition(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		
		btnBack.setOnClickListener(imgBtnListener);
	}
	
	private boolean getData() {
		String customerName = txtCustomerName.getText().toString().trim();
		if("".equals(customerName)) {
			Toast.makeText(NewCustomer.this, "客户名称不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		String addr = txtAddr.getText().toString().trim();
		if("".equals(addr)) {
			Toast.makeText(NewCustomer.this, "地址不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		String city = txtCity.getText().toString().trim();
		if("".equals(city)) {
			Toast.makeText(NewCustomer.this, "城市不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		String county = txtCounty.getText().toString().trim();
		if("".equals(county)) {
			Toast.makeText(NewCustomer.this, "县区不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		String jdb = txtJdb.getText().toString().trim();
		if("".equals(jdb)) {
			Toast.makeText(NewCustomer.this, "街道不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		String contacts = txtContacts.getText().toString().trim();
		if("".equals(contacts)) {
			Toast.makeText(NewCustomer.this, "联系人不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		String phone = txtPhone.getText().toString().trim();
		if("".equals(phone)) {
			Toast.makeText(NewCustomer.this, "电话不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		String area = txtArea.getText().toString().trim();
		if("".equals(area)) {
			Toast.makeText(NewCustomer.this, "门店面积不能为空!", Toast.LENGTH_SHORT).show();
			return false;
		}
		newCustomerInfoBean.setCustomerName(customerName);
		newCustomerInfoBean.setAddr(addr);
		newCustomerInfoBean.setCity(city);
		newCustomerInfoBean.setCounty(county);
		newCustomerInfoBean.setJdb(jdb);
		newCustomerInfoBean.setContacts(contacts);
		newCustomerInfoBean.setPhone(phone);
		newCustomerInfoBean.setArea(area);
		newCustomerInfoBean.setRemark(txtRemark.getText().toString().trim());
		newCustomerInfoBean.setYeTai(yeTaiTypeBean);
		return true;
	}
	
	private TextWatcher txtAreaListener = new TextWatcher(){

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			
		}

		public void afterTextChanged(Editable s) {
			String temp = s.toString();
			int posDot = temp.indexOf(".");
			if (posDot <= 0) return;
			if (temp.length() - posDot - 1 > 2) {
				s.delete(posDot + 3, posDot + 4);
			}
		}
	};
	
	private View.OnClickListener imgBtnListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			ImageButton imageButton = (ImageButton) v; 
			switch(imageButton.getId()) {
				case R.id.btnBack :
					NewCustomer.this.finish();
					break;
			}
		}
	};
	
	private class HttpThread extends Thread {
		private Handler handle = null;
		public HttpThread(Handler hander) {
			handle = hander;
		}
		
		public void doStart() {
			progressDialog = new ProgressDialog(NewCustomer.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}
		
		public void run() {
			Message message = handle.obtainMessage();;
			bundle = new Bundle();
			String result = null;
			CustOpeService service = new CustOpeService();
			try {
				result =  service.AddNewCust(newCustomerInfoBean, loginInfo);
				progressDialog.dismiss();
				bundle.putString("result", result);
				message.what = CommonStatic.STATE_NORMAL;
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
			message.setData(bundle);
			handle.sendMessage(message);
		}
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			String result = m.getData().getString("result");
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					new AlertDialog.Builder(NewCustomer.this)
					.setTitle("提示信息")
					.setMessage(result)
					.setNeutralButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dlg,
								int sumthin) {
							NewCustomer.this.finish();
						}
					}).show();
				break;
			}
		}
	};
}
